package com.dede.getmusic.module.download

import android.os.Environment
import android.util.Log
import com.dede.getmusic.base.BasePresenter
import com.dede.getmusic.data.SongFileInfo
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Progress
import com.lzy.okserver.OkDownload
import com.lzy.okserver.download.DownloadListener
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.jaudiotagger.audio.AudioFileIO
import org.jaudiotagger.tag.FieldKey
import java.io.File

/**
 * @author hsh
 * @date 2017/10/13 16:23.
 * @doc
 */
class DownloadPresenter(private val view: IDownloadView) : BasePresenter(view) {

    fun addFileTag(songFileInfo: SongFileInfo, t: File) {
         Observable.create<File> {
            val f = AudioFileIO.read(t)
            val tag = f.tag
            tag.setField(FieldKey.TITLE, songFileInfo.name)
            tag.setField(FieldKey.ARTIST, songFileInfo.artist)
            tag.setField(FieldKey.ALBUM, songFileInfo.album)
            f.commit()
            it.onNext(t)
        }.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                 .subscribe({
                     toast("下载完成")
                     view.cancel(songFileInfo.id)
                 }) {
                     toast("写入音乐标签错误")
                     view.cancel(songFileInfo.id)
                     it.printStackTrace()
                 }
    }

    fun startDownload(songFileInfo: SongFileInfo) {
        OkDownload.request("download", OkGo.get<File>(songFileInfo.url))
                .folder(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).absolutePath + "/get")
                .fileName(songFileInfo.fileName)
                .save()
                .register(object : DownloadListener("listener") {
                    override fun onFinish(t: File?, progress: Progress?) {
                        view.notify(songFileInfo.id, 100, "正在写入音乐标签")
                        view.downloadFinish(songFileInfo, t!!)

                        val downloadTask = OkDownload.getInstance().getTask("download")
                        downloadTask?.unRegister("listener")
                    }

                    override fun onRemove(progress: Progress?) {
                        toast("取消下载")
                        view.cancel(songFileInfo.id)
                    }

                    override fun onProgress(progress: Progress?) {
                        Log.i("download", "onProgress" + progress?.fraction)
                        val p = ((progress?.fraction ?: 0f) * 100).toInt()
                        view.notify(songFileInfo.id, p, songFileInfo.fileName)
                    }

                    override fun onError(progress: Progress?) {
                        toast("下载出错")
                        view.cancel(songFileInfo.id)
                    }

                    override fun onStart(progress: Progress?) {
                        view.notify(songFileInfo.id, 0, songFileInfo.fileName)
                    }
                }).start()
    }

}